﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Directory.Web
{
    public partial class Admin_Role : System.Web.UI.Page
    {
		protected string rolenamekey = "rolename";
        protected void Page_Load(object sender, EventArgs e)
        {
            string action = null;
            action = Request.QueryString["action"];
            if (!IsPostBack)
            {
                switch (action)
                {
                    case "delete":
                        DeleteRole();
                        break;
                    case "add":
                        break;
                    default:
                        return;
                }
            }
			lbMsg.Text = "";
        }

		// 删除角色
        private void DeleteRole()
        {
			string rolename = Request.QueryString[rolenamekey];
			try
			{
				if (RoleDataProxy.TryDeleteRole(rolename))
				{
					ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>window.alert('删除成功。');ResetRoleView();</script>");
				}
				else
				{
					ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>window.alert('删除失败，请确认是否有用户属于该角色。');ResetRoleView();</script>");
				}
			}
			catch
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>window.alert('服务器处理错误：未知错误。');ResetRoleView();</script>");
			}
			finally
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), "reset", "<script>ResetRoleView();</script>");
			}
		}
		// 创建角色
		protected void btnAdd_Click(object sender, EventArgs e)
		{
			Directory.Model.RoleInfo.ErrorCode status;
			try
			{
				RoleDataProxy.CreateRole(tbName.Text, tbDescription.Text, out status);
				switch (status)
				{
					case Directory.Model.RoleInfo.ErrorCode.Success:
						ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>window.alert('添加成功。');ResetRoleView();</script>");
						break;
					case Directory.Model.RoleInfo.ErrorCode.InvalidName:
						lbMsg.Text = "角色名格式不正确或该角色已存在！";
						break;
					default:
						ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>window.alert('添加失败。')</script>");
						break;
				}
			}
			catch
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>window.alert('服务器处理错误：未知错误。');ResetRoleView();</script>");
			}
		}
}
}
